﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using BrokerBase.Lib.Client;
using BrokerBase.Lib.Tool;

namespace Broker.Service.Model
{
    public class SClient
    {
        public static bool IsExistByName(string name,IDbConnection cnn)
        {
            try
            {
                IDbCommand cmd = cnn.CreateCommand();

                string sql = "select count(*) from Sale_Client where Name=@UserName";

                cmd.CommandText = sql;
                IDbDataParameter p = cmd.CreateParameter();
                p.ParameterName = "@UserName";
                p.DbType = DbType.String;
                p.Size = 200;
                p.Value = name;
                cmd.Parameters.Add(p);
                int r = (int)cmd.ExecuteScalar();

                return r == 0 ? false : true;
            }
            catch (Exception er)
            {
                throw er;
            }
        }

        public static SClient LoadByName(string name, IDbConnection cnn)
        {
            IDbCommand cmd = cnn.CreateCommand();

            string sql = "select id,name,IsTrial,Level from Sale_Client where Name=@UserName";

            cmd.CommandText = sql;
            IDbDataParameter p = cmd.CreateParameter();
            p.ParameterName = "@UserName";
            p.DbType = DbType.String;
            p.Size = 200;
            p.Value = name;
            cmd.Parameters.Add(p);

            IDataReader reader = cmd.ExecuteReader();

            SClient client = null;

            while (reader.Read())
            {
                client = new SClient();

                client.Id = reader.GetInt32(0);
                client.Name = reader.GetString(1);
                client.IsTrial = reader.GetBoolean(2);
                client.Level = reader.GetInt32(3);
            }

            return client;
        }

        public static SClient LoadById(int id, IDbConnection cnn)
        {
            IDbCommand cmd = cnn.CreateCommand();

            string sql = "select id,name,IsTrial,Level from Sale_Client where id=@id";

            cmd.CommandText = sql;
            IDbDataParameter p = cmd.CreateParameter();
            p.ParameterName = "@id";
            p.DbType = DbType.Int32;
            p.Value = id;
            cmd.Parameters.Add(p);

            IDataReader reader = cmd.ExecuteReader();

            SClient client = null;

            while (reader.Read())
            {
                client = new SClient();

                client.Id = reader.GetInt32(0);
                client.Name = reader.GetString(1);
                client.IsTrial = reader.GetBoolean(2);
                client.Level = reader.GetInt32(3);
            }

            return client;
        }

        public static void Save(SClient client, IDbConnection cnn)
        {
//            string sql = @"insert into sale_client(ParentId,Name,LicenseCode,EMail,CreateDate,Leaf,InviteCodeQty,Level,RegTime)
//                                values(@ParentId,@Name,@LicenseCode,@EMail,@CreateDate,1,@InviteCodeQty,@Level,@RegTime)";

//            IDbTransaction tx = cnn.BeginTransaction();

//            IDbCommand cmd = cnn.CreateCommand();
//            cmd.Transaction = tx;
//            cmd.CommandText = sql;

//            DateTime dt = DateTimeUtil.ChinaStandardTime;

//            IDbDataParameter p1 = cmd.CreateParameter();
//            p1.ParameterName = "@ParentId";
//            p1.DbType = DbType.Int32;
//            if (presenter == null)
//                p1.Value = null;
//            else
//                p1.Value = presenter.Id;
//            cmd.Parameters.Add(p1);

//            IDbDataParameter p2 = cmd.CreateParameter();
//            p1.ParameterName = "@Name";
//            p1.DbType = DbType.Int32;
//            p1.Value = registrantInfo.UserName;
//            cmd.Parameters.Add(p2);

//            IDbDataParameter p3 = cmd.CreateParameter();
//            p3.ParameterName = "@LicenseCode";
//            p3.DbType = DbType.Int32;
//            p3.Value = Guid.NewGuid().ToString();
//            cmd.Parameters.Add(p3);

//            IDbDataParameter p4 = cmd.CreateParameter();
//            p4.ParameterName = "@EMail";
//            p4.DbType = DbType.Int32;
//            p4.Value = registrantInfo.EMail;
//            cmd.Parameters.Add(p4);

//            IDbDataParameter p5 = cmd.CreateParameter();
//            p5.ParameterName = "@CreateDate";
//            p5.DbType = DbType.Int32;
//            p5.Value = dt;
//            cmd.Parameters.Add(p5);

//            IDbDataParameter p6 = cmd.CreateParameter();
//            p6.ParameterName = "@InviteCodeQty";
//            p6.DbType = DbType.Int32;
//            p6.Value = presenter.Id;
//            cmd.Parameters.Add(p6);

//            IDbDataParameter p7 = cmd.CreateParameter();
//            p7.ParameterName = "@Level";
//            p7.DbType = DbType.Int32;
//            p7.Value = presenter == null ? 1 : presenter.Level + 1;
//            cmd.Parameters.Add(p7);

//            IDbDataParameter p8 = cmd.CreateParameter();
//            p8.ParameterName = "@Level";
//            p8.DbType = DbType.Int32;
//            p8.Value = dt;
//            cmd.Parameters.Add(p8);


//            cmd.ExecuteNonQuery();
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public bool IsTrial { get; set; }

        public int Level { get; set; }

        public int? PresenterId { get; set; }

        public int? PolicyId { get; set; }



    }
}